package com.hrm.attendance.controller;

import com.hrm.attendance.service.ConfigurationService;
import com.hrm.common.controller.BaseController;
import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.domain.attendance.entity.AttendanceConfig;
import com.hrm.domain.attendance.entity.DeductionDict;
import com.hrm.domain.attendance.entity.ExtraDutyConfig;
import com.hrm.domain.attendance.entity.LeaveConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 配置考勤设置的controller
 */
@RestController
@RequestMapping("/cfg")
public class ConfigController extends BaseController {

    @Autowired
    private ConfigurationService configurationService;

    //GEN-START
    /**
     * 获取考勤设置
     * @param departmentId
     * @return
     */
    @RequestMapping(value = "/atte/item",method = RequestMethod.POST)
    public Result atteConfig(String departmentId){
        AttendanceConfig attendanceConfig = configurationService.getAtteConfig(companyId,departmentId);
        return new Result(ResultCode.SUCCESS,attendanceConfig);
    }

    /**
     * 保存或更新考勤设置
     * @param attendanceConfig
     * @return
     */
    @RequestMapping(value = "/atte",method = RequestMethod.PUT)
    public Result saveAtteConfig(@RequestBody AttendanceConfig attendanceConfig){
        attendanceConfig.setCompanyId(companyId);
        configurationService.saveAtteConfig(attendanceConfig);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 获取请假设置
     * @param departmentId
     * @return
     */
    @RequestMapping(value = "/leave/item",method = RequestMethod.POST)
    public Result leaveConfig(String departmentId){
        List<LeaveConfig> list = configurationService.getLeaveConfig(companyId,departmentId);
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     * 保存或更新请假设置
     * @param leaveConfig
     * @return
     */
    @RequestMapping(value = "/leave",method = RequestMethod.PUT)
    public Result saveLeaveConfig(@RequestBody LeaveConfig leaveConfig){
        List<LeaveConfig> list = configurationService.getLeaveConfig(leaveConfig.getCompanyId(), leaveConfig.getDepartmentId());
        for (LeaveConfig config : list) {
            config.setCompanyId(companyId);
            configurationService.saveLeaveConfig(config);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 获取扣款设置
     * @param departmentId
     * @return
     */
    @RequestMapping(value = "/deduction/item",method = RequestMethod.POST)
    public Result deductionConfig(String departmentId){
        List<DeductionDict> list = configurationService.getDeductionDict(companyId, departmentId);
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     * 保存或更新扣款设置
     * @param deductionDict
     * @return
     */
    @RequestMapping(value = "/deduction",method = RequestMethod.PUT)
    public Result saveDeductionConfig(@RequestBody DeductionDict deductionDict){
        List<DeductionDict> list = configurationService.getDeductionDict(deductionDict.getCompanyId(), deductionDict.getDepartmentId());
        for (DeductionDict config : list) {
            config.setCompanyId(companyId);
            configurationService.saveDeductionDict(deductionDict);
        }
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 获取加班设置
     * @param departmentId
     * @return
     */
    @RequestMapping(value = "/extDuty/item",method = RequestMethod.POST)
    public Result extDutyConfig(String departmentId){
        ExtraDutyConfig config = configurationService.getExtDutyConfig(companyId, departmentId);
        return new Result(ResultCode.SUCCESS,config);
    }

    /**
     * 保存或更新加班设置
     * @param extraDutyConfig
     * @return
     */
    @RequestMapping(value = "/extDuty",method = RequestMethod.PUT)
    public Result saveExtDutyConfig(@RequestBody ExtraDutyConfig extraDutyConfig){
        ExtraDutyConfig config = configurationService.getExtDutyConfig(extraDutyConfig.getCompanyId(), extraDutyConfig.getDepartmentId());
        config.setCompanyId(companyId);
        configurationService.saveExtraDutyConfig(extraDutyConfig);
        return new Result(ResultCode.SUCCESS);
    }

}
